package com.link.base.base.dailystatement.service;


import com.link.base.base.dailystatement.dao.mybatis.mapper.*;
import com.link.base.base.dailystatement.model.*;
import com.link.base.base.staticvalue.model.Staticvalue;
import com.link.base.base.staticvalue.service.StaticvalueServiceImpl;
import com.link.base.core.basic.util.YmlConstants;
import com.link.base.user.model.CoreUser;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.DateUtil;
import com.link.core.util.UserUtil;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 日结账单详情serviceImpl
 *
 * @author 安小波
 * @date 2018-08-01
 * @version 1.0
 */
@Service
public class DailyDetailServiceImpl extends BasicServiceImpl<DailyDetail> implements DailyDetailService {

    @Resource
    private KeyGenerateService keyGenerateService;
    @Resource
    private DailyDetailMapper dailyDetailMapper;
    @Resource
    private DailyInvoiceMapper dailyInvoiceMapper;
    @Resource
    private DailyInvtxnMapper dailyInvtxnMapper;
    @Resource
    private DailyReceiptMapper dailyReceiptMapper;
    @Resource
    private DailyStatementMapper dailyStatementMapper;
    @Resource
    private StaticvalueServiceImpl staticvalueService;
    /**
     * 今日收款明细类型对应的值列表类型
     */
    public static  final  String STORE_PAY_TYPE = "STORE_PAY_TYPE";
    /**
     * 今日收款明细付款方式对应的值列表类型
     */
    public static  final  String PAY_METHOD = "PAY_METHOD";
    /**
     * 今日收款明细状态对应的值列表类型
     */
    public static  final  String PAY_STATUS = "PAY_STATUS";
    /**
     * 今日开票明细状态对应的值列表类型
     */
    public static  final  String INV_CHECK_STATUS = "INVOICE_STATUS";
    /**
     * 今日开票明细类型对应的值列表类型
     */
    public static  final  String INVOICE_TYPE = "INVOICE_TYPE";
    /**
     * pdf字体文件在portalshare 目录下的路径
     */
    private static final  String  FONT_FILE_PATH = "/pdffont/msyhl.ttc,0";

    @Override
    public BasicMapper<DailyDetail> getBasicMapper() {
        return dailyDetailMapper;
    }

    /**
     * 根据headId查询日结单详情
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @Override
    public DailyDetail queryByHeadId(DailyDetail entity) throws Exception {
        if (entity.getHeadId() != null) {
            DailyDetail result = getDailyDetail(entity);
            //查询详情信息
            return result;
        } else {
            throw new ServiceException("headId不能为空");
        }

    }

    private DailyDetail getDailyDetail(DailyDetail entity) throws Exception {
        DailyInvoice dailyInvoice = new DailyInvoice();
        DailyReceipt dailyReceipt = new DailyReceipt();
        DailyDetail result = new DailyDetail();

        result = dailyDetailMapper.queryByHeadId(entity);
        dailyInvoice.setHeadId(entity.getHeadId());
        dailyReceipt.setHeadId(entity.getHeadId());
        DailyInvtxn dailyInvtxn = new DailyInvtxn();
        dailyInvtxn.setHeadId(entity.getHeadId());
        List<DailyReceipt> receiptList = dailyReceiptMapper.queryByHeadId(dailyReceipt);
        List<DailyInvoice> invoiceList = dailyInvoiceMapper.queryByHeadId(dailyInvoice);
        List<DailyInvtxn> invtxnList = dailyInvtxnMapper.queryByHeadId(dailyInvtxn);

        result.setDailyReceiptList(receiptList);
        result.setDailyInvoiceList(invoiceList);
        result.setDailyInvtxnList(invtxnList);
        return result;
    }


    /**
     * 第一次日结时查询日结单详情
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @Override
    public DailyDetail queryDailyDetails(DailyDetail entity) throws Exception {

        CoreUser user = UserUtil.getUser();
        if (user == null) {
            throw new ServiceException("PUBLIC-035");
        }
        Long orgId = user.getOrgId();
        DailyInvoice dailyInvoice = new DailyInvoice();
        //设置查询组织和仓库所有者id
        entity.setOrgId(orgId);
        entity.setAcctId(user.getAcctId());

        dailyInvoice.setOrgId(orgId);
        DailyReceipt dailyReceipt = new DailyReceipt();
        dailyReceipt.setOrgId(orgId);
        DailyInvtxn dailyInvtxn = new DailyInvtxn();
        dailyInvtxn.setOrgId(orgId);

        //设置查询时间
        dailyInvoice.setQueryDate(entity.getQueryDate());
        dailyReceipt.setQueryDate(entity.getQueryDate());
        dailyInvtxn.setQueryDate(entity.getQueryDate());
        //库存的时候设置仓库所有者acctId
        dailyInvtxn.setAcctId(user.getAcctId());

        DailyDetail result = new DailyDetail();
        result = dailyDetailMapper.queryDailyDetailHead(entity);
        if (result == null) {
            result = new DailyDetail();
        }
        List<DailyReceipt> receiptList = dailyReceiptMapper.queryReceipt(dailyReceipt);
        List<DailyInvoice> invoiceList = dailyInvoiceMapper.queryInvoice(dailyInvoice);
        List<DailyInvtxn> invtxnList = dailyInvtxnMapper.queryInvtxn(dailyInvtxn);
        result.setDailyReceiptList(receiptList);
        result.setDailyInvoiceList(invoiceList);
        result.setDailyInvtxnList(invtxnList);
        result.setDailyDate(entity.getQueryDate());
        result.setOperator(user.getFirstName());
        return result;
    }

    /**
     * 插入日结单详情头、收款明细、开票明细、库存明细
     *
     * @param entity
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void dailyDetailsInsert(DailyDetail entity) throws Exception {

        CoreUser user = UserUtil.getUser();
        if (user == null) {
            throw new ServiceException("PUBLIC-035");
        }
        if (entity.getHeadId() == null) {
            throw new ServiceException("DAILYSTATEMENT-001");
        }
        Long orgId = user.getOrgId();
        Long postnId = user.getPostnId();


        //设置组织和职位id
        entity.setOrgId(orgId);
        entity.setPostnId(postnId);
        //日结详情头的rowId
        Long detailRowId = keyGenerateService.keyGenerate();
        entity.setId(detailRowId);
        //插入日记单详情头
        dailyDetailMapper.insert(entity);

        List<DailyInvoice> invoiceList = entity.getDailyInvoiceList();
        //循环给List中的数据设置组织id、职位id、headId、rowId并插入
        for (DailyInvoice invoice : invoiceList) {
            invoice.setId(keyGenerateService.keyGenerate());
            invoice.setOrgId(orgId);
            invoice.setPostnId(postnId);
            invoice.setHeadId(entity.getHeadId());
            dailyInvoiceMapper.insert(invoice);
        }

        List<DailyReceipt> receiptList = entity.getDailyReceiptList();
        for (DailyReceipt receipt : receiptList) {
            receipt.setId(keyGenerateService.keyGenerate());
            receipt.setOrgId(orgId);
            receipt.setPostnId(postnId);
            receipt.setHeadId(entity.getHeadId());
            dailyReceiptMapper.insert(receipt);
        }

        List<DailyInvtxn> invtxnList = entity.getDailyInvtxnList();
        for (DailyInvtxn invtxn : invtxnList) {
            invtxn.setId(keyGenerateService.keyGenerate());
            invtxn.setOrgId(orgId);
            invtxn.setPostnId(postnId);
            invtxn.setHeadId(entity.getHeadId());
            dailyInvtxnMapper.insert(invtxn);
        }

        //更新日结单头状态和提交时间
        DailyStatement dailyStatement = new DailyStatement();
        dailyStatement.setId(entity.getHeadId());
        dailyStatement.setStatus("finished");
        dailyStatement.setSubmitTime(DateUtil.dateToStr(new Date(), "yyyy-MM-dd HH:mm:ss"));
        dailyStatementMapper.dailyInfoUpdate(dailyStatement);
    }

    /**
     * 将每日明细的数据存储在PDF中
     * @author guoshan
     * @date 2019/03/19
     * @param dailyDetail
     * @return  PDF自定义部分的路径
     */
    @Override
    public String printDailyBillPdf(DailyDetail dailyDetail) throws Exception {
        //创建文件
        Document document = new Document();
        PdfWriter writer = null ;

        String  filePath = YmlConstants.getTemporaryIOFileDir() ;
        String subPath = "/billaccnt/dms/";
        String fileStorePath = getStoreFilePath(dailyDetail, filePath, subPath);
        try {
            //设置字体
            String  fontPath = YmlConstants.getPortalShareFileDir() + FONT_FILE_PATH;
            BaseFont bfChinese = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            Font titleFont = new Font(bfChinese,16, Font.BOLD);
            Font commonFont = new Font(bfChinese,12, Font.NORMAL);

            writer = PdfWriter.getInstance(document, new FileOutputStream(filePath + fileStorePath));
            //打开文件
            document.open();
            CoreUser  user = UserUtil.getUser();

            //添加PDF标题
            Paragraph paragraph = new Paragraph(user.getOrgName(),titleFont);
            paragraph.setAlignment(Element.ALIGN_CENTER);
            paragraph.setSpacingAfter(10);
            document.add(paragraph);
            PdfPTable table = new PdfPTable(24);
            table.getDefaultCell().setMinimumHeight(40);

            // 单元格左内距 10px
            table.getDefaultCell().setPaddingLeft(10);

            // 宽度100%填充
            table.setWidthPercentage(100);

            //设置列宽
            float[] columnWidths = { 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f };
            table.setWidths(columnWidths);
            ArrayList<PdfPRow> rows = table.getRows();


            //设置库存变更，开票，今日收款明细
            DailyDetail  dailyDetail1 = getDailyDetail(dailyDetail);

            addCell(new String[]{"日期：" + dailyDetail1.getCreated(),"制单人：" + dailyDetail1.getOperator()},table,commonFont,24);
            addCell(new String[]{"今日销售"}, table,commonFont,24);
            addCell(new String[]{"销售金额：" + setData(dailyDetail1.getSalesAmount()) ,
                "退货金额：" + setData(dailyDetail1.getReturnAmount())},table,commonFont,24);
            addCell(new String[]{"今日收款"}, table,commonFont,24);
            addCell(new String[]{"收款金额：" + setData(dailyDetail1.getReceivedAmount())}, table,commonFont,24);
            addCell(new String[]{"银联：" + setData(dailyDetail1.getUnionpay()),
                "微信：" + setData(dailyDetail1.getWechat()),
                "支付宝：" + setData(dailyDetail1.getAlipay()),
                "现金：" + setData(dailyDetail1.getCash())}, table,commonFont,24);
            addCell(new String[]{"退款金额：" + setData(dailyDetail1.getRefundAmount())}, table,commonFont,24);
            addCell(new String[]{"银联：" + setData(dailyDetail1.getUnionpayRefund()),
                "微信：" + setData(dailyDetail1.getWechatRefund()),
                "支付宝：" + setData(dailyDetail1.getAlipayRefund()),
                "现金：" + setData(dailyDetail1.getCashRefund())}, table,commonFont,24);
            addCell(new String[]{"今日开票"}, table,commonFont,24);
            addCell(new String[]{"开票金额：" + setData(dailyDetail1.getInvoicedAmount()),
                "消票金额：" + setData(dailyDetail1.getDestroyAmount())},table,commonFont,24);
            addCell(new String[]{"今日库存"}, table,commonFont,24);
            addCell(new String[]{"当前库存：" + setData(dailyDetail1.getCurrentInvtxn()),
                "入库：" + setData(dailyDetail1.getInStock()),
                "出库：" + setData(dailyDetail1.getOutStock())},table,commonFont,24);

            //显示列表数据，列表为空则显示暂无数据
            addCell(dailyDetail1, table,commonFont,24);
            document.add(table);

            return YmlConstants.getTemporaryIOUrlDir() + fileStorePath;
        } finally {
            //关闭文档
            document.close();
            //关闭书写器
            if (writer != null) {
                writer.close();
            }
        }
    }

    /**
     * 获取PDF文件存储路径，如果上级目录不存在则新建目录
     * @author guoshan
     * @date 2019/03/19
     * @param dailyDetail   每日明细
     * @param filePath  文件存储的上级目录，从配置文件中获取
     * @param subPath   自定义目录
     * @return  有自定义目录文件名组成
     */
    private String getStoreFilePath(DailyDetail dailyDetail, String filePath, String subPath) {
        File file = new File(filePath + subPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        SimpleDateFormat datetimeFormatter = new SimpleDateFormat("yyyyMMddHHmmsss");
        String  fileName = datetimeFormatter.format(new Date()) + "_" + dailyDetail.getHeadId();
        //建立一个书写器
        return subPath + fileName + ".pdf";
    }

    /**
     * 将字符串数组中的值添加到表格中
     * @author guoshan
     * @date 2019/03/19
     * @param contentArr    需要向表格中添加的数据
     * @param table 需要添加数据的表格
     * @param font  表格中数据显示的字体
     * @param tableCollum   表格中最大列数
     */
    private void addCell(String[] contentArr, PdfPTable table, Font font, int tableCollum) {
        if (tableCollum % contentArr.length != 0) {
            throw new ServiceException("DAILYSTATEMENT-002");
        }
        int  colspan = tableCollum / contentArr.length;
        for (String content : contentArr ) {
            PdfPCell cell = getCellWithMiniHeight(content,font,colspan,null);
            table.addCell(cell);
        }

    }

    /**
     * 添加今日收款明细、今日开票明细、库存变更明细的数据到表格中
     * @author guoshan
     * @date 2019/03/19
     * @param dailyDetail 每日明细
     * @param table     要添加数据的表
     * @param commonFont    表格中数据显示字体
     * @param tableCollum   表格中最大列的值
     */
    private void addCell(DailyDetail dailyDetail, PdfPTable table, Font commonFont,int tableCollum) throws Exception {
        //设置收款明细数据
        List<DailyReceipt>  receipts = dailyDetail.getDailyReceiptList();
        List<PdfPRow> rows = table.getRows();
        addCell(new String[]{"附件一：今日收款明细"}, table,commonFont,tableCollum);
        addRecevierDetailCelll(table,commonFont);

        if (receipts == null || receipts.isEmpty()) {
            addNullCell(table, commonFont, tableCollum);
        }
        Map<String, String> typeViewVlue = getAllVuleViewMap();

        for ( DailyReceipt receipt : receipts) {
            PdfPCell[] cells = new PdfPCell[tableCollum];
            cells[0] = getCellWithMiniHeight(typeViewVlue.get(STORE_PAY_TYPE + receipt.getReceiptType()),commonFont,2,null);
            cells[1] = getCellWithMiniHeight(typeViewVlue.get(PAY_METHOD + receipt.getReceiptMethod()),commonFont,3,null);
            cells[2] = getCellWithMiniHeight(setData(receipt.getAmount()),commonFont,3,null);
            cells[3] = getCellWithMiniHeight(receipt.getReceiptDate(),commonFont,4,null);
            cells[4] = getCellWithMiniHeight(receipt.getOrderNo(),commonFont,3,null);
            cells[5] = getCellWithMiniHeight(receipt.getTransactionNum(),commonFont,3,null);
            cells[6] = getCellWithMiniHeight(typeViewVlue.get(PAY_STATUS + receipt.getReceiptStatus()),commonFont,3,null);
            cells[7] = getCellWithMiniHeight(receipt.getRemark(),commonFont,3,null);
            table.addCell(cells[0]);
            table.addCell(cells[1]);
            table.addCell(cells[2]);
            table.addCell(cells[3]);
            table.addCell(cells[4]);
            table.addCell(cells[5]);
            table.addCell(cells[6]);
            table.addCell(cells[7]);
        }

        //设置开票明细数据
        List<DailyInvoice> invoices = dailyDetail.getDailyInvoiceList();
        addCell(new String[]{"附件二：今日开票明细"}, table,commonFont,24);
        addBillDetailCelll(table,commonFont);

        //如果列表为空则添加一个空单元格
        if (invoices == null || invoices.isEmpty()) {
            addNullCell(table, commonFont, tableCollum);
        }
        for ( DailyInvoice invoice : invoices) {
            PdfPCell[] cells = new PdfPCell[tableCollum];
            cells[0] = getCellWithMiniHeight(typeViewVlue.get(INVOICE_TYPE + invoice.getInvoiceType()),commonFont,2,null);
            cells[1] = getCellWithMiniHeight(setData(invoice.getInvoiceAmount()),commonFont,3,null);
            cells[2] = getCellWithMiniHeight(invoice.getInvoiceDate(),commonFont,4,null);
            cells[3] = getCellWithMiniHeight(invoice.getInvoiceTitle(),commonFont,3,null);
            cells[4] = getCellWithMiniHeight(invoice.getTaxNum(),commonFont,3,null);
            cells[5] = getCellWithMiniHeight(invoice.getOrderNo(),commonFont,3,null);
            cells[6] = getCellWithMiniHeight(invoice.getInvoiceCode(),commonFont,3,null);
            cells[7] = getCellWithMiniHeight(typeViewVlue.get(INV_CHECK_STATUS + invoice.getInvoiceStatus()),commonFont,3,null);
            table.addCell(cells[0]);
            table.addCell(cells[1]);
            table.addCell(cells[2]);
            table.addCell(cells[3]);
            table.addCell(cells[4]);
            table.addCell(cells[5]);
            table.addCell(cells[6]);
            table.addCell(cells[7]);
        }
        //设置库存变更数据
        List<DailyInvtxn> invtxns = dailyDetail.getDailyInvtxnList();
        addCell(new String[]{"附件三：库存变更明细"}, table,commonFont,tableCollum);
        addInvtxnCell(table,commonFont);

        //如果列表为空则添加一个空单元格
        if (invtxns == null || invtxns.isEmpty()) {
            addNullCell(table, commonFont, tableCollum);
        }

        for ( DailyInvtxn invtxn : invtxns) {
            PdfPCell[] cells = new PdfPCell[tableCollum];
            cells[0] = getCellWithMiniHeight(invtxn.getProdName(),commonFont,9,null);
            cells[1] = getCellWithMiniHeight(invtxn.getProdCode(),commonFont,6,null);
            cells[2] = getCellWithMiniHeight(invtxn.getCurrentInvtxn() + "",commonFont,3,null);
            cells[3] = getCellWithMiniHeight(invtxn.getInStock() + "",commonFont,3,null);
            cells[4] = getCellWithMiniHeight(invtxn.getOutStock() + "",commonFont,3,null);
            table.addCell(cells[0]);
            table.addCell(cells[1]);
            table.addCell(cells[2]);
            table.addCell(cells[3]);
            table.addCell(cells[4]);
        }

    }

    /**
     * desc 添加收款明细表头
     * @author 刘志龙
     * @date 2019/4/3 16:42
     * @param table 要添加数据的表对象
     * @param commonFont  表中数据显示字体
     */
    private void addRecevierDetailCelll(PdfPTable table, Font commonFont) {
        PdfPCell cell = getCellWithMiniHeight("类型",commonFont,2,null);
        table.addCell(cell);
        PdfPCell cell2 = getCellWithMiniHeight("收款方式",commonFont,3,null);
        table.addCell(cell2);
        PdfPCell cell3 = getCellWithMiniHeight("收款金额",commonFont,3,null);
        table.addCell(cell3);
        PdfPCell cell4 = getCellWithMiniHeight("收款时间",commonFont,4,null);
        table.addCell(cell4);
        PdfPCell cell5 = getCellWithMiniHeight("订单编号",commonFont,3,null);
        table.addCell(cell5);
        PdfPCell cell6 = getCellWithMiniHeight("交易流水号",commonFont,3,null);
        table.addCell(cell6);
        PdfPCell cell7 = getCellWithMiniHeight("状态",commonFont,3,null);
        table.addCell(cell7);
        PdfPCell cell8 = getCellWithMiniHeight("备注",commonFont,3,null);
        table.addCell(cell8);
    }

    /**
     * desc 添加开票明细表头
     * @author 刘志龙
     * @date 2019/4/3 16:42
     * @param table 要添加数据的表对象
     * @param commonFont  表中数据显示字体
     */
    private void addBillDetailCelll(PdfPTable table, Font commonFont) {
        PdfPCell cell = getCellWithMiniHeight("类型",commonFont,2,null);
        table.addCell(cell);
        PdfPCell cell2 = getCellWithMiniHeight("开票金额",commonFont,3,null);
        table.addCell(cell2);
        PdfPCell cell3 = getCellWithMiniHeight("开票时间",commonFont,4,null);
        table.addCell(cell3);
        PdfPCell cell4 = getCellWithMiniHeight("发票抬头",commonFont,3,null);
        table.addCell(cell4);
        PdfPCell cell5 = getCellWithMiniHeight("税号",commonFont,3,null);
        table.addCell(cell5);
        PdfPCell cell6 = getCellWithMiniHeight("订单编号",commonFont,3,null);
        table.addCell(cell6);
        PdfPCell cell7 = getCellWithMiniHeight("发票号",commonFont,3,null);
        table.addCell(cell7);
        PdfPCell cell8 = getCellWithMiniHeight("状态",commonFont,3,null);
        table.addCell(cell8);
    }

    /**
     *  添加产品变更明细表表头
     * @author guoshan
     * @date 2019/03/21
     * @param table 要添加数据的表对象
     * @param commonFont  表中数据显示字体
     */
    private void addInvtxnCell(PdfPTable table, Font commonFont) {
        PdfPCell cell = getCellWithMiniHeight("产品",commonFont,9,null);
        table.addCell(cell);
        PdfPCell cell2 = getCellWithMiniHeight("产品编码",commonFont,6,null);
        table.addCell(cell2);
        PdfPCell cell3 = getCellWithMiniHeight("当前库存",commonFont,3,null);
        table.addCell(cell3);
        PdfPCell cell4 = getCellWithMiniHeight("今日入库",commonFont,3,null);
        table.addCell(cell4);
        PdfPCell cell5 = getCellWithMiniHeight("今日出库",commonFont,3,null);
        table.addCell(cell5);
    }

    /**
     * 获取一个设置了最小高度为40、多列多行合并的单元格
     * @param content 单元格的内容
     * @param commonFont 显示字体
     */
    private PdfPCell getCellWithMiniHeight(String content, Font commonFont,Integer  colspan,Integer  rowspan) {
        PdfPCell  cell = new PdfPCell();
        cell.setPhrase(new Paragraph(content,commonFont));
        if (colspan != null ) {
            cell.setColspan(colspan);
        }

        if (rowspan != null) {
            cell.setRowspan(rowspan);
        }
        cell.setMinimumHeight(40);
        //垂直居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        // 设置左边距 10
        cell.setPaddingLeft(10);

        return  cell;
    }

    /**
     * 获取一个设置了最小高度为40的单元格
     * @param content 单元格的内容
     * @param commonFont 显示字体
     */
    private PdfPCell getCellWithMiniHeight(String content, Font commonFont) {
        PdfPCell  cell = new PdfPCell();
        cell.setPhrase(new Paragraph(content,commonFont));
        cell.setMinimumHeight(40);
        //垂直居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        // 设置左边距 10
        cell.setPaddingLeft(10);
        return  cell;
    }

    /**
     * 获取类型、方式、状态的值列表的显示值，并将其存储在map中
     * @author guoshan
     * @date 2019/03/19
     * @return  map:以值列表的类型和独立源代码拼接的键，值列表的显示值为map的值
     * @throws Exception  获取值列表抛出的异常
     */
    private Map<String, String> getAllVuleViewMap() throws Exception {
        CoreUser  user = UserUtil.getUser();
        if (user == null) {
            throw new ServiceException("PUBLIC-023");
        }

        String[] types = {STORE_PAY_TYPE,PAY_METHOD,PAY_STATUS,INV_CHECK_STATUS,INVOICE_TYPE};
        Staticvalue staticvalue = new Staticvalue();
        staticvalue.setLoginCorpId(user.getLoginCorpId());

        Map<String,String> typeViewVlue = new HashMap<>();
        for (String  type : types ) {
            staticvalue.setType(type);
            List<Staticvalue> staticvalues = staticvalueService.queryByLovType(staticvalue);

            for (Staticvalue  staticvalue1 : staticvalues ) {
                typeViewVlue.put(type + staticvalue1.getVal(),staticvalue1.getName());
            }
        }
        return typeViewVlue;
    }

    /**
     * 向table中添加一个单元格，说明此表格暂时没有数据
     * @author guoshan
     * @date 2019/03/19
     * @param table 需要添加提示信息的表格
     * @param commonFont  单元格中要显示数据的字体
     * @param tableCollum 此表格本来有多少列，如果有合并单元格，则需要传入最大列。
     */
    private void addNullCell(PdfPTable table, Font commonFont, int tableCollum) {
        PdfPCell cell = new PdfPCell(new Paragraph("暂无数据",commonFont));
        cell.setColspan(tableCollum);
        //水平居中
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        //垂直居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setMinimumHeight(40);
        table.addCell(cell);
    }

    /**
     * 判断传入的值是否为空或者BigDecimal,如果是空则将其转换为空字符串，如果是BigDecimal类型则将其转换为格式良好的字符串返回
     * @author guoshan
     * @date 2019/03/21
     * @param obj 要转换的对象
     * @return 经过转换的字符串
     */
    private String  setData(Object obj) {
        String  returnValue = null;
        if ( obj instanceof BigDecimal) {
            BigDecimal bigDecimal = (BigDecimal)obj;
            bigDecimal = bigDecimal.stripTrailingZeros();

            NumberFormat format = NumberFormat.getInstance();
            format.setMinimumFractionDigits(3);
            format.setMaximumFractionDigits(2);
            format.setMaximumIntegerDigits( 10 );
            format.setMinimumIntegerDigits(1);
            returnValue = "￥" + format.format(bigDecimal);
        } else {
            returnValue = obj == null ? "0" : obj + "";
        }
        return  returnValue;
    }
}
